package com.lxyk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.pojo.Customer;
import com.lxyk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @ClassName CustomerController
 * @Description TODO
 * @Author FJQ
 * @Date 2022/10/21 15:19
 * @Version 1.0
 **/
@Controller
@RequestMapping("/cust")
@Slf4j
public class CustomerController {
    @Autowired
    private CustomerService service;


    /**
     * 在浏览器上输入要搜索的“客户名称”，点击“搜索按钮”，根据条件搜索满足条件的客户，将客户列表显示到前端页面
     *  步骤：
     *  1 调用CustomerService对象的searchCustomer方法根据客户名称搜索满足条件的客户
     *  2 将客户列表渲染到前端(客户列表)页面
     *  3 跳转到客户列表页面
     * @param pageNum
     * @param pageSize
     * @param model
     * @param customer
     * @return
     */
    @RequestMapping("/listCust")
    public String listCust(@RequestParam(required = false,defaultValue = "1",value = "pageNum")Integer pageNum,
                           @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize ,Model model,Customer customer){

        if(pageNum <=0 || pageNum.equals("") ||pageNum == null){
            pageNum = 1;
        }
        if(pageSize <= 0 || pageNum.equals("") || pageSize == null){
            pageSize = 10;
        }

        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Customer> pageInfo = service.listCust(customer, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "customerList";
    }



    /**
     * 点击按钮跳转到添加界面
     * @return
     */
    @RequestMapping("/preSaveCust")
    public String preSaveCust(){
        return "customerSave";
    }

    /**
     * 添加成功返回客户管理主页
     * @param customer
     * @return
     */
    @RequestMapping("/saveCust")
    public String add(Customer customer){
        int count = service.saveCustomer(customer);
        System.out.println("count = " + count);
        return "redirect:/cust/listCust";
    }

    /**
     * 模糊查询
     * @param custName
     * @param model
     * @return
     */
    @RequestMapping("/searchCust")
    public String searchCustomer(String custName, Model model) {
        List<Customer> custList = service.searchCustomer(custName);
        model.addAttribute("customerList",custList);
        // 把搜索条件回传到前端页面
        model.addAttribute("searchName",custName);
        return "customerList";
    }

    /**
     * 根据cid删除信息
     * @param cid
     * @return
     */
    @RequestMapping("/delCust/{cid}")
    public String deleteCustomer(@PathVariable("cid") Integer cid) {
        service.deleteCustomerById(cid);
        // 查询已删除的客户列表信息
        return "redirect:/cust/listCust";
    }

    /**
     * 根据id查询回显数据
     * @param cid
     * @param model
     * @return
     */
    @RequestMapping("/preUpdateCust/{cid}")
    public String preUpdateCustomer(@PathVariable("cid") Integer cid,Model model) {
        Customer customer = service.getCustomerById(cid);
        model.addAttribute("customer",customer);
        return "customerUpdate";
    }

    /**
     * 修改
     * @param customer
     * @return
     */
    @PostMapping("/updateCust")
     public String updateCustomer(Customer customer){
        service.updateCustomer(customer);
        return "redirect:/cust/listCust";
     }



}
